<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
// ------------------------------------------------------------------------

/**
 * Achievements_model class
 *
 * This class abstracts functions for the photo requests
 *
 * @package		Photoggle
 * @subpackage	Models
 * @category	Models
 * @author		Russ
 */
class SubmissionAchievements_model extends CI_Model {

	/* Achievement bahavior */
	var $table_name = 'a_achievement_submissions';

	function __construct()
	{
		parent::__construct();
	}


	/**
	 * Check if submission achievement exists
	 *
	 * @param	string
	 * @return	object
	 */
	function submission_achievement_exists($achievementId, $userId)
	{
		$this->db->where('achievement_id', $achievementId);
		$this->db->where('user_id', $userId);

		$query = $this->db->get($this->submission_table_name);
		if ($query->num_rows() == 1) return true;
		return false;
	}
	
	/**
	 * Fetch a users achievement progress
	 *
	 * @access	public
	 * @return	array
	 */
	function get_achievement_progress($user_id)
	{
		$sql = "
				SELECT
					a_achievement_categories.name AS achievementCategoryName,
					a_achievement_categories.id AS achievementCategoryId,
					(
						SELECT
							COUNT(*)
						FROM
							a_achievements_completed
						JOIN
							a_achievements ON a_achievements.id=a_achievements_completed.achievement_id
						WHERE
							a_achievements.category_id=a_achievement_categories.id
							AND
							a_achievements_completed.user_id = " . $user_id . "
					) AS completedAchievements,
					(
						SELECT
							COUNT(*)
						FROM
							a_achievements
						WHERE
							a_achievements.category_id=a_achievement_categories.id
					) AS totalAchievements
				FROM
					a_achievement_categories
				ORDER BY 
					(completedAchievements/totalAchievements)
				DESC
				";
		$query = $this->db->query($sql);
		return $query->result();
	}
	

 }
 ?>